<?php

require_once("track.inc.php");
require_once("database.inc.php");

$dbh = connect_db("gene");

if (is_numeric($_GET['leagueid'])) $leagueid = $_GET['leagueid'];
else die("No LeagueID provided!\n");

if (!file_exists("/home/lhoek/public_html/maps/$leagueid.png")) die ("No map for that country exists yet!\n");

$query = mysql_query("SELECT * FROM regions WHERE leagueid = $leagueid", $dbh);

$max_teams = 0;
$total_teams = 0;
$num_teams = array();
while ($row = mysql_fetch_array($query)) {
	if ($row['NumberOfUsers'] > $max_teams) $max_teams = $row['NumberOfUsers'];
	$total_teams += $row['NumberOfUsers'];
	$num_teams[$row['RegionID']] = $row['NumberOfUsers'];
}

$ci = array( #color indexes
"315" => 14,
"316" => 8,
"317" => 5,
"318" => 16,
"319" => 15,
"320" => 17,
"321" => 6,
"322" => 18,
"323" => 2,
"324" => 4,
"325" => 13,
"326" => 12,
"327" => 10,
"328" => 3,
"329" => 7,
"1263" => 11
);


$im = imagecreatefrompng("/home/lhoek/public_html/maps/$leagueid.png");

foreach (array_keys($ci) as $i) {
	$color = round(255 - (255 * ($num_teams[$i] / $max_teams)));
	imagecolorset($im, $ci[$i], $color, 255, $color);
}

header("Content-type: image/png");
imagepng($im);

?>
